package com.omega_r.omegarecyclerview.ListAdapterExample;

import com.omega_r.libs.omegarecyclerview.base.BaseProvider;
import com.omega_r.libs.omegarecyclerview.base.BaseProvider.ComponentBindingListener;
import com.omega_r.libs.omegarecyclerview.base.BaseViewHolder;
import com.omega_r.omegarecyclerview.ResourceTable;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;

public class ListAdapterAbility extends Ability implements ListAdapterViewHolder.OnclickListener {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_list_provider);
        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_recyclerview);
        BaseProvider<ListAdapterBean> provider = new BaseProvider(getContext(), ListAdapterBean.createListAdapterList(25)
                , ResourceTable.Layout_item_list_adapter);
        listContainer.setBoundaryThickness(2);
        listContainer.setBoundaryColor(new Color(Color.getIntColor("#3F51B5")));
        listContainer.setBoundarySwitch(true);
        listContainer.setHeaderBoundarySwitch(false);
        listContainer.setFooterBoundarySwitch(false);

        provider.setComponentBindingListener(new ComponentBindingListener() {
            @Override
            public BaseViewHolder creatViewHolder(Component item) {
                ListAdapterViewHolder listAdapterViewHolder = new ListAdapterViewHolder(item);
                listAdapterViewHolder.setOnclickListener(ListAdapterAbility.this);
                return listAdapterViewHolder;
            }

            @Override
            public void initData(BaseViewHolder baseViewHolder, Object object, int position) {
                ((ListAdapterViewHolder) baseViewHolder).updateView((ListAdapterBean) object);
            }
        });
        listContainer.setItemProvider(provider);
    }

    /**
     * Toast弹框
     *
     * @param content 内容
     */
    private void showToast(String content) {
        Component toastLayout = LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_layout_toast, null, false);
        Text toastText = (Text) toastLayout.findComponentById(ResourceTable.Id_text_msg_toast);
        toastText.setText(content);
        new ToastDialog(this)
                .setComponent(toastLayout)
                .setTransparent(true)
                .show();
    }

    @Override
    public void clickListener(String name) {
        showToast(String.format("onItemClick(item = %s)", name));
    }

    @Override
    public void clickLongListener(String name) {
        showToast(String.format("onItemLongClick(item = %s)", name));
    }
}
